package editor.weapon;

import java.awt.Container;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.util.LinkedList;

import javax.swing.JFileChooser;

import editor.Level;
import entity.Entity;

import math.Vector2D;

public class ActionScript
{
	public static ActionScript load( String pathname )
	{
		File f = new File( pathname );
		try
		{
			FileReader stream = new FileReader( f );
			StreamTokenizer buf = new StreamTokenizer( stream );
			
			LinkedList<Function> functions = new LinkedList<Function>();
			
			while( StreamTokenizer.TT_EOF != buf.nextToken() )
			{
				if( buf.sval.equals( "MoveTo" ) )
				{
					buf.nextToken();
					double x = buf.nval;
					buf.nextToken();
					double y = buf.nval;

					functions.add( new Function.MoveTo( new Vector2D( x, y ) ) );
				}
				else if( buf.sval.equals( "SetAngle" ) )
				{
					buf.nextToken();
					functions.add( new Function.SetAngle( buf.nval ) );
				}
				else if( buf.sval.equals( "Pause" ) )
				{
					buf.nextToken();
					functions.add( new Function.Pause( buf.nval ) );
				}
			}
			
			stream.close();
			
			return new ActionScript( functions );
		} 
		catch ( FileNotFoundException e )
		{
			e.printStackTrace();
		} 
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static ActionScript load( Container frame )
	{
		JFileChooser fc = new JFileChooser();
		fc.setCurrentDirectory( new File( "C:/Documents and Settings/Owner/Desktop/Projects/Level Editor/Levels" ) );
		int returnVal = fc.showOpenDialog( frame );
		
		if( returnVal == JFileChooser.APPROVE_OPTION )
		{
			return load( fc.getSelectedFile().getAbsolutePath() );
		}
			
		return null;
	}
	
	private int index;
	private LinkedList<Function> functions;
	
	public ActionScript( LinkedList<Function> f )
	{
		index = 0;
		functions = f;
	}
	
	/**
	 * 
	 * @param e the entity that will be changed based on the script
	 * @return true if the Script is starting over; false otherwise
	 */
	public boolean runScript( Entity e )
	{
		Function f; 
		
		while( !( ( f = functions.get( index ) ) instanceof Function.Pause ) )
		{
			if( f instanceof Function.MoveTo )
			{
				e.setPosition( ((Function.MoveTo)f).function() );
			}
			else if( f instanceof Function.SetAngle )
			{
				e.setAngle( ((Function.SetAngle)f).function() );
			}
		
			index++;
		}

		index++;
		if( index == functions.size() )
		{
			index = 0;
			return true;
		}
		
		return false;
	}
	
	public void save( String name )
	{
		File f = new File( "ActionScripts/" + name + ".txt" );
		
		try 
		{ 
			f.createNewFile();
			BufferedWriter buf = new BufferedWriter( new FileWriter( f.getAbsolutePath() ) );
			
			String s = "";
			for( Function func : functions )
			{
				if( func instanceof Function.MoveTo )
				{
					Vector2D v = ((Function.MoveTo)func).function();
					s += "MoveTo " + v.getX() + " " + v.getY();
				}
				else if( func instanceof Function.SetAngle )
				{
					s += "SetAngle " + ((Function.SetAngle)func).function();
				}
				else if( func instanceof Function.Pause )
				{
					s += "Pause " + ((Function.Pause)func).function();
				}
				s += "\n";
			}
			
			buf.write( s );
			
			buf.close();
		} 
		catch ( FileNotFoundException err ) 
		{
			err.printStackTrace();
		} 
		catch ( IOException err ) 
		{
			err.printStackTrace();
		}
	}
}
